package gov.fema.hazus.bdc.server.servlet;

import gov.fema.hazus.bdc.server.servlet.DAO;
import gov.fema.hazus.bdc.shared.entity.Address;
import gov.fema.hazus.bdc.shared.entity.Bracing;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Capacity;
import gov.fema.hazus.bdc.shared.entity.Contact;
import gov.fema.hazus.bdc.shared.entity.EarthquakeBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.EarthquakeHazard;
import gov.fema.hazus.bdc.shared.entity.EarthquakeSecondaryCharacteristics;
import gov.fema.hazus.bdc.shared.entity.Economy;
import gov.fema.hazus.bdc.shared.entity.FloodBasement;
import gov.fema.hazus.bdc.shared.entity.FloodBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.FloodHazard;
import gov.fema.hazus.bdc.shared.entity.Location;
import gov.fema.hazus.bdc.shared.entity.ManufacturedHousing;
import gov.fema.hazus.bdc.shared.entity.Roof;
import gov.fema.hazus.bdc.shared.entity.WallsAndOpenings;
import gov.fema.hazus.bdc.shared.entity.WindBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.WindHazard;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions;
import com.google.appengine.api.labs.taskqueue.TaskOptions.Method;
import com.google.inject.Singleton;
import com.googlecode.objectify.OQuery;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

@Singleton
public class SearchBuildingsObjectify extends HttpServlet {

	public static final String TASK_PATH = "/task/objectify/buildings/search";

	private final Logger logger = Logger.getLogger(SearchBuildingsObjectify.class);
    private Cache cache;

    private DAO dao = new DAO();
    
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	 throws ServletException, IOException {
		
		String searchTerm = req.getParameter("q");
		
		try {
			
			Objectify ofy = ObjectifyService.begin();
			OQuery<Building> q = ObjectifyService.createQuery(Building.class);
			q.filter("terms == ", searchTerm);
			for (Building building: ofy.prepare(q).asIterable()) {
				resp.getWriter().println(building.toString());
			    logger.debug(building.toString());
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	 }
	
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
        
    	res.setContentType("text/plain");
		String searchTerm = req.getParameter("q");
		
		try {
			
			Objectify ofy = ObjectifyService.begin();
			OQuery<Building> q = ObjectifyService.createQuery(Building.class);
			q.filter("terms == ", searchTerm.toUpperCase());
			for (Building building: ofy.prepare(q).asIterable()) {
				
				// now that we have the ID, fetch the other entities
				Address address = ofy.get(Address.class, building.getId());
				logger.debug(address.toString());
				building.setAddress(address);
				
				res.getWriter().println(building.toString());
			    logger.debug(building.toString());
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	
    	
    }
    
	
}
